package cn.itcast.crawler.jd.component;

import cn.itcast.crawler.jd.pojo.Item;
import cn.itcast.crawler.jd.service.ItemService;
import cn.itcast.crawler.jd.util.HttpUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import java.io.IOException;
import java.util.Date;
import java.util.List;

@Component
public class ItemTask {
    @Autowired
    private HttpUtils httpUtils;
    @Autowired
    private ItemService itemService;

    public static final ObjectMapper MAPPER = new ObjectMapper();

    @Scheduled(fixedDelay = 1000*100)
    @Transactional
    public void process() throws IOException {
        String url="https://list.jd.com/list.html?cat=9987%2C653%2C655&ev=exbrand_%E5%8D%8E%E4%B8%BA%EF%BC%88HUAWEI%EF%BC%89%5E&pvid=91af4c03e4144ae5891b2631b888c938&s=117&click=0&page=";
        for (int i = 0; i < 2; i++) {
            String html=this.httpUtils.getHtml(url+i);
            this.parseHtml(html);
        }
        System.out.println("执行完成");
    }

    private void parseHtml(String html) throws IOException {
        //使用jsonp解析页面
         Document document= Jsoup.parse(html);
         //获取商品数据
        Long spuId = null;
        Elements spus=document.select("div#J_goodsList > ul > li");
        for (Element spuEle:spus
             ) {
            //获取spuID

            String spuEl=spuEle.attr("data-spu");
            if(spuEl!=null&&!spuEl.equals("")){
                 spuId=Long.parseLong(spuEl);
            }

            //获取skuId
            Elements skus=document.select("li.ps-item img");
            for (Element skuEle:skus
                 ) {
                Long skuId=Long.parseLong(skuEle.attr("data-sku"));
                //判断商品是否被抓取过
                Item param=new Item();
                param.setSku(skuId);
                List<Item> list=itemService.findAll(param);
                //判断是否查询到结果
                if(list.size()>0){
                    continue;
                }
                Item item=new Item();
                item.setSpu(spuId);
                item.setSku(skuId);
                item.setCreated(new Date());
                item.setUpdated(item.getCreated());
                item.setUrl("https://item.jd.com/" + skuId + ".html");
                //获取商品图片
                String picUrl="https:"+skuEle.select("img[data-sku]").first().attr("data-lazy-img")
                        .replace("/n7/","/n1/");
                //有些数据是没有图片的，这里进行下过滤
                if(picUrl.equals("https:")){
                    break;
                }
                String picName=httpUtils.getImage(picUrl);
                item.setPic(picName);
                //获取商品价格
                String priceJson=this.httpUtils.getHtml("https://p.3.cn/prices/mgets?skuIds=J_"+skuId);
                Double price= MAPPER.readTree(priceJson).get(0).get("p").asDouble();
                item.setPrice(price);

                //获取商品标题
                String itemInfo=this.httpUtils.getHtml(item.getUrl());
                String title=Jsoup.parse(itemInfo).select("div.sku-name").text();
                item.setTitle(title);

                itemService.save(item);
            }
        }

    }

}
